<?php

/**
 * ECSHOP 管理中心文章处理程序文件
* ============================================================================
* * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
* 网站地址: http://www.ecshop.com；
* ----------------------------------------------------------------------------
* 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
* 使用；不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* $Author: liubo $
* $Id: article.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . "includes/fckeditor/fckeditor.php");
require_once(ROOT_PATH . 'includes/cls_image.php');

/*初始化数据交换对象 */
$exc   = new exchange($ecs->table("news_comment"), $db, 'id', 'comment');
//$image = new cls_image();

/*------------------------------------------------------ */
//-- 帖子下的评论
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	/* 取得过滤条件 */
	$filter = array();
	$smarty->assign('ur_here',      $_LANG['02_news_comment_list']);
	$smarty->assign('action_link',  array('text' => $_LANG['01_news_list'], 'href' => 'news.php?act=list'));
	$smarty->assign('full_page',    1);
	$smarty->assign('filter',       $filter);

	$comment_list = get_comment_list();

	$smarty->assign('comment_list',    $comment_list['row']);
	$smarty->assign('filter',          $comment_list['filter']);
	$smarty->assign('record_count',    $comment_list['record_count']);
	$smarty->assign('page_count',      $comment_list['page_count']);

// 	$sort_flag  = sort_flag($comment_list['filter']);

	assign_query_info();
	$smarty->display('news_comment.htm');
}
/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif($_REQUEST['act']=="query"){
	$comment_list = get_comment_list();
	$smarty->assign('comment_list',  $comment_list['row']);
	$smarty->assign('filter',       $comment_list['filter']);
	$smarty->assign('record_count', $comment_list['record_count']);
	$smarty->assign('page_count',   $comment_list['page_count']);
	make_json_result($smarty->fetch('news_comment.htm'), '',array('filter' => $comment_list['filter'], 'page_count' => $comment_list['page_count']));
}

elseif ($_REQUEST['act'] == 'remove')
{
	$id = intval($_GET['id']);
	$exc->drop($id);
	$url = 'news_comment.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
	ecs_header("Location: $url\n");
	exit;
}
/* 获得文章列表 */
function get_comment_list()
{
	$result = get_filter();
	if ($result === false)
	{
		$filter = array();
		$filter['news_id']    = empty($_REQUEST['news_id']) ? '' : trim($_REQUEST['news_id']);
		$filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'p1.comment_time' : trim($_REQUEST['sort_by']);
		$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

		$where = '';
		if ($filter['news_id'])
		{
			$where .= " AND news_id = ".$filter['news_id'];
		}

		/* 评论总数 */
		$sql = 'SELECT COUNT(*) FROM ' .$GLOBALS['ecs']->table('news_comment'). 
				'WHERE 1=1 ' .$where;
		$filter['record_count'] = $GLOBALS['db']->getOne($sql);

		$filter = page_and_size($filter);

		/* 获取文章数据 */
		$sql = 'SELECT p1.id,p1.comment_time,p1.comment,p2.nickname as name '.
				'FROM ' .$GLOBALS['ecs']->table('news_comment'). ' p1 '.
				'LEFT JOIN `wxch_user` p2 ON p1.commenter = p2.uid '.
				'WHERE 1=1 ' .$where. ' ORDER BY '.$filter['sort_by'].' '.$filter['sort_order']." LIMIT ". $filter['start'] .", " . $filter['page_size'];
		set_filter($filter, $sql);
	}
	else
	{
		$sql    = $result['sql'];
		$filter = $result['filter'];
	}
	$row = $GLOBALS['db']->getAll($sql);
	//获取
	foreach ($row as $k=>$v){
		$row[$k]['comment_time'] = date($GLOBALS['_CFG']['time_format'], $v['comment_time']);
	}
	
	$arr = array('row' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
	return $arr;
}

?>